package com.cgtong.android.fragment;


import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.cgtong.android.R;
import com.cgtong.android.activity.MonthReportActivity;
import com.cgtong.android.activity.TimeLimitActivity;
import com.cgtong.android.adapter.ProblemCountDailyAdapter;
import com.cgtong.android.adapter.ProblemCountMonthYearAdapter;
import com.cgtong.android.base.BaseLazyFragment;
import com.cgtong.android.bean.DayReportBean;
import com.cgtong.android.bean.MonthYearReportBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.ProblemCountContract;
import com.cgtong.android.mvp.presenter.ProblemCountPresenter;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.ReportPopupWindow;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhouyou.http.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 问题统计
 */
public class CountProblemsFragment extends BaseLazyFragment implements ReportPopupWindow.OnItemClickListener, ProblemCountContract.View {


    @BindView(R.id.current_report_txt)
    TextView mCurrentReportTxt;
    @BindView(R.id.select_report_ll)
    LinearLayout mSelectReportLl;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;

    private ReportPopupWindow popupWindow;

    private ProblemCountDailyAdapter mDailyAdapter;
    private ProblemCountMonthYearAdapter mMonthYearAdapter;
    private List<DayReportBean> mDayList;
    private List<MonthYearReportBean> mMonthYearList;

    private ProblemCountContract.Presenter mPresenter;

    private int currentReport = 0;//0:日报 1:月报 2：年报

    public static CountProblemsFragment newInstance() {
        Bundle args = new Bundle();
        CountProblemsFragment fragment = new CountProblemsFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_count_problems;
    }

    @Override
    public void initData() {
        popupWindow = new ReportPopupWindow(getActivity());
        popupWindow.setItemClickListener(this);

        mPresenter = new ProblemCountPresenter(this);
        mDayList = new ArrayList<>();
        mMonthYearList = new ArrayList<>();
        initRecyclerView();
    }

    @Override
    protected void loadDataByNet() {
        mPresenter.httpDayReportData();
    }

    @Override
    protected void onDataReloadByNet(View v) {

    }

    @OnClick(R.id.select_report_ll)
    public void onViewClicked() {
        popupWindow.showPopupWindow(mSelectReportLl);
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (currentReport == 0) {
                    mPresenter.httpDayReportData();
                } else if (currentReport == 1) {
                    String time = TimeUtils.millis2String(System.currentTimeMillis(), ConstantManager.YYYY_MM_FORMAT);
                    LogUtils.d("time --- " + time);
                    mPresenter.httpMonthReportData(time, true);
                } else {
                    mPresenter.httpYearReportData();
                }
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //----------日报---------
        mDailyAdapter = new ProblemCountDailyAdapter(mDayList);
//        mDailyAdapter.disableLoadMoreIfNotFullPage(mRecyclerView);
        mRecyclerView.setAdapter(mDailyAdapter);
        mDailyAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mDailyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //-------------
            }
        });
        mDailyAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onLoadMoreEnd(false);
                    }
                }, 100);
            }
        }, mRecyclerView);
        //----------月报和年报---------
        mMonthYearAdapter = new ProblemCountMonthYearAdapter(mMonthYearList);
        mMonthYearAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mMonthYearAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (currentReport == 1) {
                    LogUtils.e("onItemClick--------------" + position);
                    TimeLimitActivity.start(getActivity(), mMonthYearAdapter.getItem(position).getDatetime());
                } else if (currentReport == 2) {
                    MonthReportActivity.start(getActivity(), mMonthYearAdapter.getItem(position).getDatetime());
                }
            }
        });
        mMonthYearAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (currentReport == 1) {
                    String maxTime = mMonthYearAdapter.getItem(mMonthYearAdapter.getItemCount() - 2).getDatetime();
                    mPresenter.httpMonthReportData(maxTime, false);
                } else {
                    mRecyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            onLoadMoreEnd(false);
                        }
                    }, 100);
                }
            }
        }, mRecyclerView);

    }

    @Override
    public void onItemClick(int position) {
        if (position == 0) {//日报
            currentReport = 0;
            mPresenter.httpDayReportData();
            mCurrentReportTxt.setText(getString(R.string.txt_daily_report));
            mRecyclerView.setAdapter(mDailyAdapter);

        } else if (position == 1) {//月报
            currentReport = 1;
            String time = TimeUtils.millis2String(System.currentTimeMillis(), ConstantManager.YYYY_MM_FORMAT);
            LogUtils.d("time --- " + time);
            mPresenter.httpMonthReportData(time, true);
            mCurrentReportTxt.setText(getString(R.string.txt_monthly_report));
            mMonthYearAdapter.setMonthReport(true);
            mRecyclerView.setAdapter(mMonthYearAdapter);

        } else if (position == 2) {//年报
            currentReport = 2;
            mPresenter.httpYearReportData();
            mCurrentReportTxt.setText(getString(R.string.txt_yearly_report));
            mMonthYearAdapter.setMonthReport(false);
            mRecyclerView.setAdapter(mMonthYearAdapter);


        }
    }

    //------------回调---------------

    @Override
    public void setRefreshing(boolean isRefresh) {
        if(mSwipeLayout!=null){
            mSwipeLayout.setRefreshing(isRefresh);
        }
    }

    @Override
    public void setEnableLoadMore(boolean enableLoadMore) {
        if (currentReport == 0) {
            mDailyAdapter.setEnableLoadMore(enableLoadMore);
        } else {
            mMonthYearAdapter.setEnableLoadMore(enableLoadMore);
        }
    }

    @Override
    public void onLoadMoreEnd(boolean isLoadMoreEnd) {
        LogUtils.e("onLoadMoreEnd--------------" + isLoadMoreEnd + " currentReport= " + currentReport);
        if (currentReport == 0) {
            mDailyAdapter.loadMoreEnd(isLoadMoreEnd);
        } else {
            mMonthYearAdapter.loadMoreEnd(isLoadMoreEnd);
        }
    }


    @Override
    public void onError(ApiException e) {
        if (currentReport == 0) {
            mDailyAdapter.setNewData(null);
            mDailyAdapter.setEmptyView(R.layout.layout_error);
        } else {
            mMonthYearAdapter.setNewData(null);
            mMonthYearAdapter.setEmptyView(R.layout.layout_error);
        }

    }

    @Override
    public void onNullData() {
        PostUtil.postSuccessDelayed(loadService);
        if (currentReport == 0) {
            mDailyAdapter.setNewData(null);
            mDailyAdapter.setEmptyView(R.layout.layout_empty);
        } else {
            mMonthYearAdapter.setNewData(null);
            mMonthYearAdapter.setEmptyView(R.layout.layout_empty);
        }
    }

    @Override
    public void onHttpDayReportDataSuccess(List<DayReportBean> datas) {
        PostUtil.postSuccessDelayed(loadService);
        mDailyAdapter.setNewData(datas);
        mDailyAdapter.disableLoadMoreIfNotFullPage(mRecyclerView);
    }

    @Override
    public void onHttpMonthReportDataSuccess(List<MonthYearReportBean> datas, boolean isRefresh) {
        if (isRefresh) {
            mMonthYearAdapter.setNewData(datas);
        } else {
            mMonthYearAdapter.addData(datas);
        }
        mMonthYearAdapter.loadMoreComplete();

    }


    @Override
    public void onHttpYearReportDataSuccess(List<MonthYearReportBean> list) {
        mMonthYearAdapter.setNewData(list);
        mMonthYearAdapter.loadMoreComplete();

    }
}
